package operations;

import MyThrowable.IDRepeatWrongful;
import MyThrowable.InputWrongful;
import student.Student;
import student.StudentList;

import java.util.Scanner;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * Description:添加新生
 * User: 86183
 * Date: 2022-12-14
 * Time: 7:41
 */
public class AddOperation implements IOperation{
    Scanner scan = new Scanner(System.in);
    static String PatternStr1 = "^[\\u4e00-\\u9fa5]{2,10}$";
    static String PatternStr2 = "^[0-9]+$";
    @Override
    public void work(StudentList studentList){
        System.out.println("新增学生！");
        String id = Addid(studentList);
        String name = AddName(studentList);
        int age = AddAge(studentList);
        scan.nextLine();
        String ms = AddMs(studentList);
        String gender = AddGender(studentList);
        int currentSize = studentList.getUsedsize();
        Student student = new Student(Integer.valueOf(id), name, ms, age, gender);
        //添加学生
        studentList.addStudents(student);
        //学生有效个数+1
        studentList.setUsedsize(currentSize + 1);
        System.out.println("新增成功!");

    }

    private int AddAge(StudentList studentList) {
        System.out.println("请输入新学生的年龄(17~80)：");
        while (true){
            int age = scan.nextInt();
            try {
                if (age < 17 || age > 80) {
                    throw new InputWrongful("输入有误,请再次输入!");
                }else {
                    return age;
                }
            } catch (InputWrongful e) {
                e.printStackTrace();
            }
        }
    }

    public String Addid(StudentList studentList){
        System.out.println("请输入新学生的学号:(只能为0~9的数字)");
      while (true) {
          String id = scan.nextLine();
          try {
              if (!Pattern.matches(PatternStr2, id)) {
                  throw new InputWrongful("输入有误,请再次输入!");
              } else if (studentList.isIDReapt(id)) {
                  throw new IDRepeatWrongful("ID重复,请重新输入!");
              }
          } catch (IDRepeatWrongful e) {
              e.printStackTrace();
          } catch (InputWrongful e) {
              e.printStackTrace();
          }
          if (Pattern.matches(PatternStr2, id) && !studentList.isIDReapt(id)) {
              return id;
          }
      }
    }
    public String AddName(StudentList studentList){
        System.out.println("请输入新学生的名字(限制汉字个数2~10)：");
        while (true) {
            String name = scan.nextLine();
            try {
                if (!Pattern.matches(PatternStr1, name)) {
                    throw new InputWrongful("输入有误,请再次输入!");
                }else {
                    return name;
                }
            } catch (InputWrongful e) {
                e.printStackTrace();
            }
        }
    }
    public String AddMs(StudentList studentList){
        System.out.println("请输入新学生的专业(限制汉字个数2~10)：");
        while (true) {
            String ms = scan.nextLine();
            try {
                if (!Pattern.matches(PatternStr1, ms)) {
                    throw new InputWrongful("输入有误,请再次输入!");
                }else {
                    return ms;
                }
            } catch (InputWrongful e) {
                e.printStackTrace();
            }
        }
    }
    public String AddGender(StudentList studentList){
        System.out.println("请输入新学生的性别(M/F)：");
        while (true) {
            String gender = scan.nextLine();
            try {
                if ((!gender.equals("F")) && (!gender.equals("M"))) {
                    throw new InputWrongful("输入有误,请再次输入!");
                }else {
                    return gender;
                }
            } catch (InputWrongful e) {
                e.printStackTrace();
            }
        }
    }

}
